﻿using System.Collections.Generic;
using System.Diagnostics;

namespace RockBus.PerformanceCounters.Interfaces
{
    public class RockBusPerformanceCounterMetadata
    {
        public const string CategoryName = "RockBus";
        public const string CategoryHelp = "The RockBus PerformanceCounter Category";

        private const string _executingFormat = "{0}.Executing";
        private const string _completedFormat = "{0}.Completed";
        private const string _failedFormat = "{0}.Failed";
        private const string _completionRateFormat = "{0}.CompletionRate";
        private const string _failureRateFormat = "{0}.FailureRate";

        public static string GetExecutingCounterName(string operationName)
        {
            return string.Format(_executingFormat, operationName);
        }

        public static string GetCompletedCounterName(string operationName)
        {
            return string.Format(_completedFormat, operationName);
        }

        public static string GetFailedCounterName(string operationName)
        {
            return string.Format(_failedFormat, operationName);
        }

        public static string GetCompletionRateCounterName(string operationName)
        {
            return string.Format(_completionRateFormat, operationName);
        }

        public static string GetFailureRateCounterName(string operationName)
        {
            return string.Format(_failureRateFormat, operationName);
        }

        public static IEnumerable<string> GetCounterNames(string operationName)
        {
            yield return GetExecutingCounterName(operationName);
            yield return GetCompletedCounterName(operationName);
            yield return GetFailedCounterName(operationName);
            yield return GetCompletionRateCounterName(operationName);
            yield return GetFailureRateCounterName(operationName);
        }

        public static IEnumerable<CounterCreationData> GetCounterCreationDatas(string operationName)
        {
            yield return new CounterCreationData(
                GetExecutingCounterName(operationName),
                string.Format("Number of executing {0} operations", operationName),
                PerformanceCounterType.NumberOfItems32);

            yield return new CounterCreationData(
                GetCompletedCounterName(operationName),
                string.Format("Number of completed {0} operations", operationName),
                PerformanceCounterType.NumberOfItems32);

            yield return new CounterCreationData(
                GetFailedCounterName(operationName),
                string.Format("Number of failed {0} operations", operationName),
                PerformanceCounterType.NumberOfItems32);

            yield return new CounterCreationData(
                GetCompletionRateCounterName(operationName),
                string.Format("Number of completed {0} operations per second", operationName),
                PerformanceCounterType.RateOfCountsPerSecond32);

            yield return new CounterCreationData(
                GetFailureRateCounterName(operationName),
                string.Format("Number of failed {0} operations per second", operationName),
                PerformanceCounterType.RateOfCountsPerSecond32);
        }
    }
}
